package com.letv.util;

import java.nio.file.Paths;
import java.sql.Connection;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.letv.common.ConnectionManager;
import com.letv.common.Constants;
import com.letv.common.LoadDict;
import com.letv.common.Mappings;
import com.letv.conf.ConfigFactory;
import com.letv.mq.MQ;
import com.letv.mq.MQLeso;
import com.letv.mq.MqPipLineAddMssAlbum;
import com.letv.mq.MqPipLineAddMssVideo;

public class SystemInitListener implements ServletContextListener {
	private static Logger logger = LoggerFactory.getLogger(SystemInitListener.class);

	@Override
	public void contextInitialized(ServletContextEvent sce) {

		String webPah = sce.getServletContext().getRealPath("/");
		Constants.WEB_PATH = webPah;
        String log4jPath = Paths.get(webPah, "WEB-INF", "classes", "log4j.properties").toString();
        PropertyConfigurator.configure(log4jPath);
        logger.info("Offline Server BIGIN......................");
        String configPath = Paths.get(webPah, "WEB-INF", "classes", "config.xml").toString();
		ConfigFactory.init(configPath);
		String pinyinMapPath = webPah + "WEB-INF/classes";
		logger.info(pinyinMapPath);
		LoadDict.initPinyinMap(pinyinMapPath);
		LoadDict.initStarPinyinMap(pinyinMapPath);
		String isopen = ConfigFactory.getString("mq.isopen");
        ConnectionManager cm = null;
        Connection connToSrc = null;
        Connection connToLeso = null;
        try {
            cm = ConnectionManager.getInstance();
            connToSrc = cm.getConnectionFromMMS();
            connToLeso = cm.getConnectionFromLeso();
            SQLUtil.setDictionary(connToSrc);
            SQLUtil.setStar(connToSrc);
            SQLUtil.setRAlbumMap(connToSrc);
            SQLUtil.setCagegoryMapping(connToLeso);
        } catch (Exception e) {
            logger.error(",init dictionary and star map error!", e);
        } finally {
            SQLUtil.close(connToSrc);
            SQLUtil.close(connToLeso);
        }
        String ifDaquan = ConfigFactory.getString("daquan.isopen");
        if(!StringUtils.isBlank(ifDaquan) && "1".equals(ifDaquan)) {
            Constants.IF_DAQUAN = true;
            logger.info("use config for daquan");
        } else {
            Constants.IF_DAQUAN = false;
            logger.info("use config not for daquan");
        }
        Mappings.initDisplayAreaMap(ConfigFactory.getString("leso.displayArea"));
		if (StringUtils.isNotBlank(isopen) && isopen.equals("1")) {
			MQ.start();// 启动消息队列接收器
			logger.info("MQ.start ...");
        }
		String isopenMssPipLine = ConfigFactory.getString("mqMssPipline.isopen");
		if (StringUtils.isNotBlank(isopenMssPipLine) && isopenMssPipLine.equals("1")) {
			MqPipLineAddMssAlbum.start();
			logger.info("MqPipLineAddMss album start ...");
			MqPipLineAddMssVideo.start();
			logger.info("MqPipLineAddMss video start ...");
        }
		String isopen1 = ConfigFactory.getString("mqleso.isopen");
		if (StringUtils.isNotBlank(isopen1) && isopen1.equals("1")) {
		    MQLeso.start();// 启动消息队列接收器
		    logger.info("MQLeso.start ...");
		}
		logger.info("Offline Server SUCCESS......................");

	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("Offline Server DESTROY......................");
	}
}
